package com.CRApp.service.impl;

import com.CRApp.mapper.AddressMapper;
import com.CRApp.pojo.Address;
import com.CRApp.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: AddressServiceImpl
 * @Author : GXXbrandy
 * Description : 收货地址服务接口实现类
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 添加收货地址
     * 创建新的收货地址，地址ID由数据库自动生成
     *
     * @param address 收货地址对象
     * @return 新增地址的ID，失败返回null
     */
    @Override
    @Transactional
    public Integer addAddress(Address address) {
        if (address == null || address.getUserId() == null) {
            return null;
        }
        
        // 验证必要字段
        if (address.getRecipientName() == null || address.getRecipientName().trim().isEmpty() ||
            address.getPhone() == null || address.getPhone().trim().isEmpty() ||
            address.getAddress() == null || address.getAddress().trim().isEmpty()) {
            return null;
        }
        
        try {
            // 执行插入，利用MyBatis的useGeneratedKeys特性自动获取ID
            addressMapper.insert(address);
            return address.getAddressesId();
        } catch (Exception e) {
            // 记录异常并返回null
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 更新收货地址
     * 更新指定ID的收货地址信息
     *
     * @param address 包含更新内容的地址对象
     * @return 更新结果，true表示成功，false表示失败
     */
    @Override
    @Transactional
    public boolean updateAddress(Address address) {
        if (address == null || address.getAddressesId() == null || address.getUserId() == null) {
            return false;
        }
        
        try {
            // 先检查地址是否存在
            Address existingAddress = addressMapper.selectById(address.getAddressesId());
            if (existingAddress == null) {
                return false;
            }
            
            // 验证地址所有权
            if (!existingAddress.getUserId().equals(address.getUserId())) {
                return false;
            }
            
            // 执行更新
            return addressMapper.update(address) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除收货地址
     * 根据地址ID删除指定的收货地址
     *
     * @param addressesId 地址ID
     * @param userId 用户ID (仅作权限验证)
     * @return 删除结果，true表示成功，false表示失败
     */
    @Override
    @Transactional
    public boolean deleteAddress(Integer addressesId, Integer userId) {
        if (addressesId == null || userId == null) {
            return false;
        }
        
        try {
            // 验证地址是否存在
            Address address = addressMapper.selectById(addressesId);
            if (address == null) {
                return false;
            }
            
            // 验证地址所有权
            if (!address.getUserId().equals(userId)) {
                return false;
            }
            
            // 执行删除
            return addressMapper.deleteById(addressesId) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取用户的所有收货地址
     * 查询指定用户ID的所有收货地址
     *
     * @param userId 用户ID
     * @return 地址列表，无地址或失败返回空列表
     */
    @Override
    public List<Address> getUserAddresses(Integer userId) {
        if (userId == null) {
            return new ArrayList<>();
        }
        
        try {
            return addressMapper.selectByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    /**
     * 获取收货地址详情
     * 查询指定ID的收货地址详情
     *
     * @param addressesId 地址ID
     * @return 地址对象，不存在返回null
     */
    @Override
    public Address getAddressDetail(Integer addressesId) {
        if (addressesId == null) {
            return null;
        }
        
        try {
            return addressMapper.selectById(addressesId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
} 